package com.example.zhinongbao.ui;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zhinongbao.R;
import com.example.zhinongbao.bean.Good;
import com.example.zhinongbao.utils.Constant;
import com.example.zhinongbao.utils.ToastUtils;

/**
 * 商品页面
 */
public class GoodFragment extends Fragment {
    // 商品标题
    private TextView text_label;
    // 商品详细内容描述
    private TextView text_content;
    // 商品价格
    private TextView text_price;
    // 商品产地
    private TextView text_address;
    // 商品主页图
    private ImageView image_good;
    // 当前商品
    private Good good;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_good, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        good = Constant.currentGood;
        initView();
    }

    private void initView() {
        image_good = getView().findViewById(R.id.image_good);
        text_label = getView().findViewById(R.id.text_label);
        text_content = getView().findViewById(R.id.text_content);
        text_price = getView().findViewById(R.id.text_price);
        text_address = getView().findViewById(R.id.text_address);

        image_good.setImageURI(Uri.parse(good.getImageUri()));
        text_label.setText(good.getLabel());
        text_content.setText(good.getContent());
        text_price.setText(good.getPrice());
        text_address.setText("产地：" + good.getAddress());

        getView().findViewById(R.id.btn_buy_car).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 把当前商品加到全局变量的购物车中，以便在购物车页面显示
                Constant.currentCar.add(good);
                ToastUtils.getInstance().makeToast("加购成功！");
            }
        });
    }

}
